// refund.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: false,
    appColor: app.appColor
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoadings();
    this.setData({
      code: options.code,
    })

    this.money(options.code, options.y);
  },

  // 退款金额
  money: function (code, y) {
    var that = this;
    let url = y == true ? 'CarRental/CarRentalQueryRefundFee' : 'WxMicro/QueryRefundFee';
    wx.request({
      url: app.serviceurl + url,
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: { OrderCode: code },
      dataType: 'json',
      success: function (res) {
        if(!res.data.Result){
          wx.showModal({
            content: res.data.Message,
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.navigateBack({})
              }
            }
          });
        }
        that.setData({
          money: res.data
        });

        app.hideLoadings();
      },
      fail: function (res) {
        console.log(res)
        app.hideLoadings();
      }
    })
  },

  // 退款
  refund: function (code, y) {
    var that = this;
    let url = y == true ? 'CarRental/CarRentalRefund' : 'WxMicro/WxRefundTicket';
    try{
      this.setData({
        disabled: true
      })
      app.showLoadings();
      wx.request({
        url: app.serviceurl + url,
        header: {
          Authorization: "Bearer " + app.accesstoken
        },
        data: { OrderCode: code },
        dataType: 'json',
        success: function (res) {
          if (!res.data.Result) {
            wx.showModal({
              content: res.data.Message,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateBack({})
                }
              }
            });
          }
          app.hideLoadings();
          that.setData({
            refund: res.data
          });

          wx.setStorage({
            key: 'refunds',
            data: res.data
          });

          if (res.data.Result == true) {
            wx.redirectTo({
              url: '../paySuccess/paySuccess',
            })
          }
        },
        fail: function (res) {
          console.log(res)
        },
        complete: function (res) {
          if (res) {
            var time = setTimeout(function () {
              that.setData({
                disabled: false
              });
            }, 1000)
          }
        }
      })
    }catch(err){console.log(err)}
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'menu') {
      return app.OnShare();
    }
  },
  backto: function () {
    this.refund(this.data.code)

  },
})